package com.tarena.lbs.marketing.repository.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.marketing.mapper.ActivityMapper;
import com.tarena.lbs.marketing.repository.ActivityRepository;
import com.tarena.lbs.pojo.marketing.bo.MarketingActivityBO;
import com.tarena.lbs.pojo.marketing.dos.MarketingActivityDO;
import com.tarena.lbs.pojo.marketing.query.MarketingActivityQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

@Repository
public class ActivityRepositoryImpl implements ActivityRepository {
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public BasePageList<MarketingActivityBO> getActivitiesByPage(MarketingActivityQuery query) {
        //分页查询 转化封装
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<MarketingActivityDO> dos=activityMapper.selectActivityByPage(query);
        PageInfo<MarketingActivityDO> pageInfo=new PageInfo<>(dos);
        BasePageList<MarketingActivityBO> boPage=null;
        if (pageInfo.getTotal()>0){
            //转化bo
            List<MarketingActivityBO> bos=pageInfo.getList().stream().map(mDO->{
                MarketingActivityBO bo=new MarketingActivityBO();
                BeanUtils.copyProperties(mDO,bo);
                return bo;
            }).collect(Collectors.toList());
            boPage=new BasePageList<>(bos,pageInfo.getTotal());
        }else{
            boPage=new BasePageList<>(null,0L);
        }
        return boPage;
    }

    @Override
    public Integer activitySave(MarketingActivityBO bo) {
        MarketingActivityDO mDo=new MarketingActivityDO();
        //转化
        BeanUtils.copyProperties(bo,mDo);
        activityMapper.insert(mDo);
        return mDo.getId();
    }

    @Override
    public MarketingActivityBO getActivitiesById(Integer id) {
        MarketingActivityDO aDo=activityMapper.selectById(id);
        MarketingActivityBO bo=new MarketingActivityBO();
        if (aDo!=null){
            BeanUtils.copyProperties(aDo,bo);
        }
        return bo;
    }
}
